package com.xzq.bugstack;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.nio.charset.Charset;
import java.util.Optional;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/19 10:30
 * @Version 1.0.0
 **/
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        //解码器
        //基于换行符号
//        channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        //基于指定字符串[换行符，这样功能等同于LineBasedFrameDecoder]
//        channel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, false, Delimiters.lineDelimiter()));
        //基于最大长度
        channel.pipeline().addLast(new FixedLengthFrameDecoder(4));
        channel.pipeline().addLast(new StringDecoder(Charset.forName("gbk")));
        //接受数据的Handler
        channel.pipeline().addLast(new MyServerHandler());
    }

}
